/**
 * @overview 创建面或体
 * @author dengqiang
 * @description 原生Cesium polygon
 * @update  2022.03.21
 */
class GPolygon {
  constructor() {
    window.GPolygonArray = []
  }

  //新增单个体或面
  add(paramObj) {
    const { name, option, parentName } = paramObj
    // let hierarchy = this.changeXYArrayToCar3(option.polygon.position)
    // let position = Cesium.Cartesian3.fromDegrees(
    //   option.centerpos[0],
    //   option.centerpos[1],
    //   option.centerpos[2]
    // )
    // let optionModified = {
    //   positions: option.position,
    //   // attribute: option.attribute,
    //   name: option.name || '',
    //   fill: option.fill || true,
    //   fillColor: option.fillColor || 	"rgba(255, 255, 255, 0.1)",
    //   outline: option.outline || true,
    //   outlineColor: option.outlineColor || "rgba(0, 153, 255, 1.0)",
    //   outlineWidth: option.outlineWidth || 1,
    //   closeTop: option.closeTop || false,
    //   closeBottom: option.closeBottom || false,
    //   extrudedHeight: option.extrudedHeight || 0,
    //   height: option.height || 0
    // };

    // let polygon = new CgsPolygon(optionModified);
    // polygon.feature3D.attribute = option.attribute; //支持添加自定义属性

    // let polygonObject = {
    //   name: option.name,
    //   parentName: parentName,
    //   polygon: polygon
    // }
    // window.GPolygonArray.push(polygonObject)
    // window.mapCancelAutoRotate.addFeature(polygon);
    // return polygon;

    //原生方式
    // let entity = {
    //   id: option.id || undefined,
    //   name: option.name || "",
    //   position: option.centerpos,
    //   label: {
    //     text: option.label.text || "",
    //     font: option.label.font || "18px san-serif",
    //     pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
    //     distanceDisplayCondition:
    //       option.label.distanceDisplayCondition ||
    //       new Cesium.DistanceDisplayCondition(0, 6000),
    //     disableDepthTestDistance: 10000,
    //     showBackground: true, //是否显示背景
    //     backgroundColor:
    //       option.label.backgroundColor ||
    //       new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
    //     backgroundPadding:
    //       option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
    //     fillColor: option.label.fillColor || Cesium.Color.WHITE, //填充色
    //     outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0), //外线颜色
    //     outlineWidth: option.label.outlineWidth || 1.0, //外线宽
    //   },
    //   polygon: {
    //     hierarchy: new Cesium.CallbackProperty(function (time, result) {
    //       var hierarchyTemp = new Cesium.PolygonHierarchy(option.points, null);
    //       return hierarchyTemp;
    //     }, false),
    //     material:
    //       Cesium.Color.fromCssColorString(option.polygon.color) ||
    //       Cesium.Color.fromCssColorString("rgba(185, 221, 185, 0.8)"),
    //     perPositionHeight: true,
    //     distanceDisplayCondition:
    //       option.polygon.distanceDisplayCondition ||
    //       Cesium.DistanceDisplayCondition(10, 1000),
    //     extrudedHeight: option.polygon.extrudedHeight || 60,
    //   },
    // };

    const entity = {
      id: option.id || undefined,
      name: option.name || '',
      position: option.centerpos,
      label: {
        text: option.label.text || '',
        font: option.label.font || '18px san-serif',
        pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
        distanceDisplayCondition:
          option.label.distanceDisplayCondition || new Cesium.DistanceDisplayCondition(0, 6000),
        disableDepthTestDistance: 10000,
        showBackground: true, //是否显示背景
        backgroundColor: option.label.backgroundColor || new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
        backgroundPadding: option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
        fillColor: option.label.fillColor || Cesium.Color.WHITE, //填充色
        outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0.8), //外线颜色
        outlineWidth: option.label.outlineWidth || 1.0, //外线宽
      },
      polygon: {
        hierarchy: new Cesium.CallbackProperty(function (time, result) {
          const hierarchyTemp = new Cesium.PolygonHierarchy(option.points, null)
          return hierarchyTemp
        }, false),
        material:
          option.polygon.color || Cesium.Color.fromCssColorString('rgba(185, 221, 185, 0.8)'),
        // perPositionHeight: true,
        outline: true,
        outlineColor: Cesium.Color.GRAY.withAlpha(0.7),
        outlineWidth: 1.0,
        distanceDisplayCondition:
          option.polygon.distanceDisplayCondition || Cesium.DistanceDisplayCondition(10, 1000),
        // extrudedHeight: option.polygon.extrudedHeight || 60,
      },
    }
    //如果传了绝对高度 说明创建的要素是体
    if (option.polygon.extrudedHeight) {
      entity.polygon.perPositionHeight = true
      entity.polygon.extrudedHeight = option.polygon.extrudedHeight
    }

    const polygon = window.map3DControl.viewer.entities.add(entity)
    polygon.attribute = option.attribute //支持添加自定义属性
    const polygonObject = {
      name: option.name,
      parentName: parentName,
      polygon: polygon,
    }
    window.GPolygonArray.push(polygonObject)
    return polygon
  }

  //根据name移除单个面或体
  removeByName(name) {
    window.GPolygonArray.forEach((item, index) => {
      if (item.name == name) {
        window.GPolygonArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.polygon)
      }
    })
  }

  //根据parentName移除一批面或体
  removeByPatName(parentName) {
    const length = window.GPolygonArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GPolygonArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GPolygonArray[i].polygon)
        window.GPolygonArray.splice(i, 1)
      }
    }
  }

  //移除所有面或体
  removeAll() {
    window.GPolygonArray.forEach((item) => {
      window.map3DControl.viewer.entities.removeAll()
    })
    window.GPolygonArray = []
  }

  //根据name控制单个面或体的显隐
  setVisibleByName(name, boolean) {
    window.GPolygonArray.forEach((item) => {
      if (item.name == name) {
        item.polygon.show = boolean
      }
    })
  }

  //根据parentName控制一批面或体的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GPolygonArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.polygon.show = boolean
      }
    })
  }

  changeXYArrayToCar3(array) {
    const car3Array = []
    array.forEach((ele) => {
      car3Array.push(ele[0])
      car3Array.push(ele[1])
      car3Array.push(ele[2])
    })
    const res = Cesium.Cartesian3.fromDegreesArrayHeights(car3Array)
    return res
  }
}

export default GPolygon
